package app.netant.book.shuoshuo520;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class ShuoShuo520Detail extends NetAntBase {
	
	private ShuoShuo520Entity entity;

	public ShuoShuo520Detail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(ShuoShuo520Entity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		Elements activePart = document.select("#main .box .title b a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
		
	}
	
	public void analyseTitle(){
		Elements activePart = document.select("#main .box .book_info #info h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title);
		}
	}
	
	public void analyseMemo(){
		Elements activePart = document.select("#main .box .book_info #info h3.bookinfo_intro");
		if(activePart.size()>0){
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}
	
	public void analyseTags(){
		
		Elements activePart = document.select("#main .box .book_info #info .options span");
		for(int i=0;i<activePart.size();i++){
			
			Element activeEle = activePart.get(i);
			String activeStr = activeEle.text();
			if(activeStr.startsWith("作者")){
				String writer = activeStr.substring(3);
				System.out.println("【Writer】:"+writer);
				this.entity.setNovelWriter(writer);
			}
			else if(activeStr.startsWith("共")){
				String size = activeStr.substring(1);
				System.out.println("【Size】:"+size);
				this.entity.setNovelSize(size);
			}
			
		}
		
	}
	
	public void analyseDate(){
		Elements activePart = document.select("#main .box .book_info #info .update");
		if(activePart.size()>0){
			Element activeEle = activePart.get(0);
			String cutStr = this.cutString(activeEle.html(), '<', '>');
			int st = cutStr.lastIndexOf("（");
			int ed = cutStr.lastIndexOf("）");
			if(st > 0 && ed > 0 && ed > st){
				String date = cutStr.substring(st+1,ed);
				System.out.println("【Date】:"+date);
				this.entity.setNovelDate(date);
			}
		}
		
	}
	
	public void analyseHref(){
		Elements activePart = document.select("#main .box .book_info #info .options a");
		for(int i=0;i<activePart.size();i++){
			Element activeEle = activePart.get(i);
			String activeStr = activeEle.text().trim();
			if("TXT下载".equals(activeStr)){
				String href = activeEle.attr("href");
				if(href.startsWith("/")){
					href = "http://www.shuoshuo520.com"+href;
				}
				System.out.println("【Href】:"+href);
				this.entity.setDownPath(href);
				break;
			}
		}
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseMemo();
		this.analyseTags();
		this.analyseDate();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		ShuoShuo520Detail detail = new ShuoShuo520Detail("http://www.shuoshuo520.com/book/38/38268/");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseMemo();
		detail.analyseTags();
		detail.analyseDate();
		detail.analyseHref();
	}
	
}
